package com.dz.controller.guide;

import com.dz.bean.Work;
import com.dz.service.BookingService;
import com.dz.service.TransactionService;
import com.dz.util.Result;
import com.dz.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/guide/work")
public class WorkController {
    @Autowired
    TransactionService transactionService;

    @Autowired//获取当前登录用户
    private HttpServletRequest request;

    // 获取地陪的工作记录（已支付的预约）
    @GetMapping("/records")
    public Result getRecords() {
        try {
            String token = request.getHeader("token");
            if (token == null || !TokenUtil.verify(token)) {
                return Result.error(400,"认证失败");
            }

            Integer guideId = TokenUtil.getId(token);
            System.out.println(guideId);
            if (guideId == null) {
                return Result.error(400,"无法获取用户信息");
            }

            // 查询该地陪所有工作记录
            List<Work> workRecords = transactionService.find(guideId);
            System.out.println(workRecords);

            return Result.success(workRecords);
        } catch (Exception e) {
            log.error("获取工作记录失败：", e);
            return Result.error(400,"获取工作记录失败："+e.getMessage());
        }
    }

}

